package com.ctg.payroll2.action.social;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.filter.social.PbSocialDetailFilter;
import com.ctg.payroll2.bean.filter.social.PbSocialMasterFilter;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.bean.po.social.PbSocialDetail;
import com.ctg.payroll2.bean.po.social.PbSocialMaster;
import com.ctg.payroll2.bean.po.social.RecomTemplateSocialItemSearchBean;
import com.ctg.payroll2.service.social.PbSocialItemService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 社保项目管理
 * @author 鲁力 2009-9-1 下午02:46:10
 *
 */
public class PbSocialItemAction extends ActionSupport {
	/***********************属性begin**************************/
	private static final long serialVersionUID = -3520946378392623632L;
	/**
	 * 返回给页面的标记
	 */
	private int msg;
	/**
	 * 主表翻页结果
	 */
	private Page pbSocialMasterResult;
	/**
	 * 查询条件
	 */
	private PbSocialMasterFilter masFilter;
	/**
	 * 查询条件
	 */
	private PbSocialDetailFilter detailFilter;
	/**
	 * 编辑社保项目和新增社保项目的entity
	 */
	private PbSocialMaster pbSocialMaster;
	/**
	 * 用到的service,用spring注入
	 */
	private PbSocialItemService pbSocialItemService;
	/**
	 * 子表翻页结果
	 */
	private Page pbSocialDetailResult;
	/**
	 * 社保项目子表对象
	 */
	private PbSocialDetail pbSocialDetail;
	/**
	 * 页面选择的id数组,action中接收为string
	 */
	private String masterIds;
	/**
	 * 页面选择的id数组,action中接收为string
	 */
	private String subIds;
	
	private List<RecomTemplateSocialItemSearchBean> beanList;
	/***********************属性end**************************/
	public String querySocialItemByFilter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] s = request.getParameterValues("masFilter.socialSecurityIds");
		if (s!=null && s.length>0) {
			StringBuffer temp = new StringBuffer();
			for (int i = 0; i < s.length; i++) {
				temp.append(s[i]).append(",");
			}
			masFilter.setSocialSecurityIds(temp.substring(0,temp.length()-1));
		}
		beanList = pbSocialItemService.getPbSocialItemByFilter(masFilter);
		return SUCCESS;
	}
	
	public String findSocialItemByCompanCode(){
		SysUser user = this.getCurrentUser();
		try {
			beanList = pbSocialItemService.getSocialItemByCompanCode(user.getCompanyCode());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String findSocialItemByEmps(){
		SysUser user = this.getCurrentUser();
		try {
			beanList = pbSocialItemService.getSocialItemByEmps(user.getCompanyCode(), masFilter.getIds());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 点击页面查询按钮
	 * @return 翻页查询的结果
	 */
	public String queryPbSocialMasterResult() {
		HttpServletRequest request = ServletActionContext.getRequest();
		if(masFilter==null){
			masFilter=new PbSocialMasterFilter();
		}
		masFilter.setStartIndex(Parse.toInteger(request.getParameter("start") , 0));
		masFilter.setEndIndex(Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE));
		pbSocialMasterResult = pbSocialItemService.getPbSocialMasterPageByFilter(masFilter);
		return SUCCESS;
	}
	/**
	 * 子表的翻页查询
	 * @return
	 */
	public String queryPbSocialDetailResult() {
		pbSocialDetailResult = pbSocialItemService.getPbSocialDetailPageByFilter(detailFilter);
		return SUCCESS;
	}
	/**
	 * 新增按钮
	 * @return
	 */
	//我还是希望新增，修改，删除的数据能分开处理，减少网路传输、数据库资源的浪费。Bean里我分成3个数组addList，updList，delList。
	//新增里只用到addList
	public String addPbSocialMaster(){
		SysUser user = getCurrentUser();
		HttpServletRequest request = ServletActionContext.getRequest();
		String modified = request.getParameter("modified");
		List<PbSocialDetail> modifieds=new ArrayList<PbSocialDetail>();
		JSONArray modifiedArray = JSONArray.fromObject(modified);
		for (int i = 0; i < modifiedArray.size(); i++) {
			   Object o = modifiedArray.get(i);
			   JSONObject jsonObject = JSONObject.fromObject(o);
			   PbSocialDetail pbSocialDetail = (PbSocialDetail) JSONObject.toBean(jsonObject, PbSocialDetail.class);
			   pbSocialDetail.setCreate_code(user.getEmployeeCsCode());
			   pbSocialDetail.setCreator_id(user.getEmployeeId());
			   pbSocialDetail.setCreator_ip(user.getLoginIP());
			   modifieds.add(pbSocialDetail);
			}
		initPbSocialMaster(pbSocialMaster,user);
		pbSocialItemService.insertMasterAndDetail(pbSocialMaster);
		return SUCCESS;
	}
	private SysUser getCurrentUser(){
		return (SysUser)ServletActionContext.getRequest().getSession().getAttribute(Constants.CURRENT_USER);
	}
	/**
	 * 点击页面删除按钮
	 * @return
	 */
	public String deleteSocialMaster(){
		pbSocialItemService.deletePbSocialMasterById(pbSocialMaster.getSocial_security_id());
		return SUCCESS;
	}
	/**
	 * 编辑社保项目时先要运行的
	 * @return
	 */
	//返回都用detailList
	public String retrieveSocialSecurity(){
		pbSocialMaster = pbSocialItemService.getMasterAndDetailByMasterId(pbSocialMaster.getSocial_security_id());
		return SUCCESS;
	}
	/**
	 * 更新社保项目主信息
	 * @return
	 */
	public String updateSocialMaster(){
		SysUser user = getCurrentUser();
		HttpServletRequest request = ServletActionContext.getRequest();
		String modified = request.getParameter("modified");
		List<PbSocialDetail> addfieds=new ArrayList<PbSocialDetail>();
		List<PbSocialDetail> modifieds=new ArrayList<PbSocialDetail>();
		JSONArray modifiedArray = JSONArray.fromObject(modified);
		for (int i = 0; i < modifiedArray.size(); i++) {
		   Object o = modifiedArray.get(i);
		   JSONObject jsonObject = JSONObject.fromObject(o);
		   PbSocialDetail pbSocialDetail = (PbSocialDetail) JSONObject.toBean(jsonObject, PbSocialDetail.class);
		   if(pbSocialDetail.getSub_social_security_id()==null){
			   pbSocialDetail.setSocial_security_id(pbSocialMaster.getSocial_security_id());
			   pbSocialDetail.setCreate_code(user.getEmployeeCsCode());
			   pbSocialDetail.setCreator_id(user.getEmployeeId());
			   pbSocialDetail.setCreator_ip(user.getLoginIP());
			   addfieds.add(pbSocialDetail);
		   }else{
			   pbSocialDetail.setModifier_code(user.getEmployeeCsCode());
			   pbSocialDetail.setModifier_id(user.getEmployeeId());
			   pbSocialDetail.setModifier_ip(user.getLoginIP());
			   modifieds.add(pbSocialDetail);
		   }
		}
		String deleted = request.getParameter("deleted");
		List<PbSocialDetail> deleteds=new ArrayList<PbSocialDetail>();
		JSONArray deletedArray = JSONArray.fromObject(deleted);
		for (int i = 0; i < deletedArray.size(); i++) {
		   Object o = deletedArray.get(i);
		   JSONObject jsonObject = JSONObject.fromObject(o);
		   PbSocialDetail pbSocialDetail = (PbSocialDetail) JSONObject.toBean(jsonObject, PbSocialDetail.class);
		   deleteds.add(pbSocialDetail);
		}
		pbSocialMaster.setAddList(addfieds);
		pbSocialMaster.setUpdList(modifieds);
		pbSocialMaster.setDelList(deleteds);
		initPbSocialMaster(pbSocialMaster,user);
		pbSocialItemService.updatePbSocialMaster(pbSocialMaster);
		return SUCCESS;
	}
	/**
	 * 更新社保子表信息
	 * @return
	 */
	public String updateSocialDetail(){
		pbSocialItemService.updatePbSocialDetail(pbSocialDetail);
		return SUCCESS;
	}
	/**
	 * 删除子表一条记录
	 * @return
	 */
	public String deleteSocialDetail(){
		pbSocialItemService.deletePbSocialDetailById(pbSocialDetail.getSub_social_security_id());
		return SUCCESS;
	}
	/**
	 * 删除子表多条条记录
	 * @return
	 */
	public String deleteSocialDetailMutil(){
		pbSocialItemService.deletePbSocialDetailByIds(subIds);
		return SUCCESS;
	}
	/**
	 * 删除主表多条条记录
	 * @return
	 */
	//是否做级联删除
	public String deleteSocialMasterMutil(){
		pbSocialItemService.deletePbSocialMasterByIds(masterIds);
		return SUCCESS;
	}

	private PbSocialMaster initPbSocialMaster(PbSocialMaster pbSocialMaster, SysUser u) {
		pbSocialMaster.setCreate_code(u.getEmployeeCsCode());
		pbSocialMaster.setCreator_id(u.getEmployeeId());
		pbSocialMaster.setCreator_ip(u.getLoginIP());
		pbSocialMaster.setModifier_code(u.getEmployeeCsCode());
		pbSocialMaster.setModifier_id(u.getEmployeeId());
		pbSocialMaster.setModifier_ip(u.getLoginIP());
		return pbSocialMaster;
	}
	/************************************************************
	 * 
	 * getter, setter 方法
	 * 
	 ***********************************************************/
	/**
	 * @return 返回masFilter
	 */
	public PbSocialMasterFilter getMasFilter() {
		return masFilter;
	}

	/**
	 * @param masFilter 设置masFilter
	 */
	public void setMasFilter(PbSocialMasterFilter masFilter) {
		this.masFilter = masFilter;
	}

	/**
	 * @return 返回msg
	 */
	public int getMsg() {
		return msg;
	}

	/**
	 * @param msg 设置msg
	 */
	public void setMsg(int msg) {
		this.msg = msg;
	}

	/**
	 * @return 返回pbSocialDetail
	 */
	public PbSocialDetail getPbSocialDetail() {
		return pbSocialDetail;
	}

	/**
	 * @param pbSocialDetail 设置pbSocialDetail
	 */
	public void setPbSocialDetail(PbSocialDetail pbSocialDetail) {
		this.pbSocialDetail = pbSocialDetail;
	}

	/**
	 * @return 返回pbSocialDetailResult
	 */
	public Page getPbSocialDetailResult() {
		return pbSocialDetailResult;
	}

	/**
	 * @param pbSocialDetailResult 设置pbSocialDetailResult
	 */
	public void setPbSocialDetailResult(Page pbSocialDetailResult) {
		this.pbSocialDetailResult = pbSocialDetailResult;
	}

	/**
	 * @param pbSocialItemService 设置pbSocialItemService
	 */
	public void setPbSocialItemService(PbSocialItemService pbSocialItemService) {
		this.pbSocialItemService = pbSocialItemService;
	}

	/**
	 * @return 返回pbSocialMaster
	 */
	public PbSocialMaster getPbSocialMaster() {
		return pbSocialMaster;
	}

	/**
	 * @param pbSocialMaster 设置pbSocialMaster
	 */
	public void setPbSocialMaster(PbSocialMaster pbSocialMaster) {
		this.pbSocialMaster = pbSocialMaster;
	}

	/**
	 * @return 返回pbSocialMasterResult
	 */
	public Page getPbSocialMasterResult() {
		return pbSocialMasterResult;
	}

	/**
	 * @param pbSocialMasterResult 设置pbSocialMasterResult
	 */
	public void setPbSocialMasterResult(Page pbSocialMasterResult) {
		this.pbSocialMasterResult = pbSocialMasterResult;
	}
	/**
	 * @return 返回detailFilter
	 */
	public PbSocialDetailFilter getDetailFilter() {
		return detailFilter;
	}
	/**
	 * @param detailFilter 设置detailFilter
	 */
	public void setDetailFilter(PbSocialDetailFilter detailFilter) {
		this.detailFilter = detailFilter;
	}
	/**
	 * @return 返回masterIds
	 */
	public String getMasterIds() {
		return masterIds;
	}
	/**
	 * @param masterIds 设置masterIds
	 */
	public void setMasterIds(String masterIds) {
		this.masterIds = masterIds;
	}
	/**
	 * @return 返回subIds
	 */
	public String getSubIds() {
		return subIds;
	}
	/**
	 * @param subIds 设置subIds
	 */
	public void setSubIds(String subIds) {
		this.subIds = subIds;
	}
	/**
	 * @return 返回beanList
	 */
	public List<RecomTemplateSocialItemSearchBean> getBeanList() {
		return beanList;
	}
	/**
	 * @param beanList 设置beanList
	 */
	public void setBeanList(List<RecomTemplateSocialItemSearchBean> beanList) {
		this.beanList = beanList;
	}
}
